<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Deal extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function order()
	{
		if ($this->user['paypwd']) {
			$paypwd = $this->input->post('paypwd');
			if (!$paypwd || !$this->pwd->verify($paypwd, $this->user['paypwd'])) {
				ajax_return(1, '安全密码错误');
			}
		}

		$coin_id = intval($this->input->post('coin_id'));

		if ($coin_id <= 0) {
			ajax_return(1, '找不到币种');
		}

		$coin = $this->db->where('id', $coin_id)->get('coin')->row_array();

		if (!$coin) {
			ajax_return(1, '找不到币种');
		}

		$hour = date('h', time());

		if ($coin['is_trade'] == 0) {
			ajax_return(1, '交易系统关闭中');
		}

		if (($hour < $coin['open_time']) || ($hour < $coin['close_time'])) {
			ajax_return(1, '现在是休市时间');
		}

		$price = num($this->input->post('price'), 2);
		$num = num($this->input->post('num'), 4);
		$type = intval($this->input->post('type'));
		$total = num($price * $num, 2);

		if ($price <= 0) {
			ajax_return(1, '请输入价格');
		}

		if ($price < $coin['low_price']) {
			ajax_return(1, '价格不能低于' . $coin['low_price']);
		}

		if ($coin['high_price'] < $price) {
			ajax_return(1, '价格不能高于' . $coin['high_price']);
		}

		if ($num <= 0) {
			ajax_return(1, '请输入数量');
		}

		if (!in_array($type, array(0, 1))) {
			ajax_return(1, '订单信息有误，请联系客服');
		}

		if ($total <= 0) {
			ajax_return(1, '总额太低');
		}

		if ($type == 0) {
			$this->db->where('id', $this->session->user_id);
			$user = $this->db->get('user')->row_array();
			if (($user['cny'] <= 0) || ($user['cny'] < $total)) {
				ajax_return(1, '资金不足');
			}

			$this->db->set('cny', 'cny-' . $total, false);
			$this->db->set('cny_frozen', 'cny_frozen+' . $total, false);
			$this->db->where('id', $this->user['id']);
			$this->db->update('user');
		}
		else {
			$this->db->where('user_id', $this->user['id']);
			$this->db->where('coin_id', $coin_id);
			$xnb = $this->db->get('xnb')->row_array();
			if (($xnb['num'] <= 0) || ($xnb['num'] < $num)) {
				ajax_return(1, '币不足');
			}

			$this->db->set('num', 'num-' . $num, false);
			$this->db->set('num_frozen', 'num_frozen+' . $num, false);
			$this->db->where('user_id', $this->session->user_id);
			$this->db->where('coin_id', $coin_id);
			$this->db->update('xnb');
		}

		$data['price'] = $price;
		$data['num'] = $num;
		$data['coin_id'] = $coin_id;
		$data['type'] = $type;
		$data['user_id'] = $this->user['id'];
		$data['add_time'] = time();
		$rs = $this->db->insert('orders', $data);

		if ($rs) {
			$this->run($coin, $type, $price, $this->db->insert_id());
			ajax_return(0, '提交成功');
		}
		else {
			ajax_return(1, '提交失败');
		}
	}

	public function run($coin, $type, $price, $id)
	{
		$coin_id = $coin['id'];
		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_user write,t_xnb write,t_orders write,t_deal write');
		$rs = $this->db->query('select * from t_deal order by add_time desc');
		$last = $rs->row_array();
		$last_price = $last['price'];

		if ($type == 0) {
			$rs = $this->db->query('select *,(num-deal) as num from t_orders where price<=' . $price . ' and coin_id=' . $coin_id . ' and type=1 and status=0 order by price asc,add_time asc,num desc');
		}
		else {
			$rs = $this->db->query('select *,(num-deal) as num from t_orders where price>=' . $price . ' and coin_id=' . $coin_id . ' and type=0 and status=0 order by price desc,add_time asc,num desc');
		}

		$orders = $rs->result_array();

		if (!$orders) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			return false;
		}

		$r = array();

		foreach ($orders as $v) {
			$this->db->where('id', $id);
			$this->db->where('status', 0);
			$rs = $this->db->get('orders');
			$order = $rs->row_array();

			if (!$order) {
				break;
			}

			$deal_num = min($v['num'] - $v['deal'], $order['num'] - $order['deal']);

			if ($deal_num <= 0) {
				break;
			}

			$d = array();

			if ($type == 0) {
				if ($price < $last_price) {
					$deal_price = $price;
				}
				else if ($last_price < $v['price']) {
					$deal_price = $v['price'];
				}
				else {
					$deal_price = $last_price;
				}

				$d['buy_user_id'] = $order['user_id'];
				$d['sell_user_id'] = $v['user_id'];
				$d['buy_orders_id'] = $order['id'];
				$d['sell_orders_id'] = $v['id'];
			}
			else {
				if ($v['price'] < $last_price) {
					$deal_price = $v['price'];
				}
				else if ($last_price < $price) {
					$deal_price = $price;
				}
				else {
					$deal_price = $last_price;
				}

				$d['buy_user_id'] = $v['user_id'];
				$d['sell_user_id'] = $order['user_id'];
				$d['buy_orders_id'] = $v['id'];
				$d['sell_orders_id'] = $order['id'];
			}

			$deal_total = num($deal_price * $deal_num, 2);

			if ($deal_total <= 0) {
				break;
			}

			$cny_fee = num(($deal_total * $coin['sell_fee']) / 100, 2);
			$xnb_fee = num(($deal_num * $coin['buy_fee']) / 100, 4);
			$d['cny_fee'] = $cny_fee;
			$d['xnb_fee'] = $xnb_fee;
			$d['price'] = $deal_price;
			$d['num'] = $deal_num;
			$d['total'] = $deal_total;
			$d['coin_id'] = $coin_id;
			$d['status'] = 1;
			$d['type'] = $d['sell_orders_id'] < $d['buy_orders_id'] ? 1 : 0;
			$d['add_time'] = time();
			$r[] = $this->db->insert('deal', $d);
			$r[] = $this->db->set('cny', 'cny+' . num($deal_total - $cny_fee, 2), false)->where('id', $d['sell_user_id'])->update('user');
			$r[] = $this->db->set('cny_frozen', 'cny_frozen-' . $deal_total, false)->where('id', $d['buy_user_id'])->update('user');
			$r[] = $this->db->set('num', 'num+' . num($deal_num - $xnb_fee, 4), false)->where('user_id', $d['buy_user_id'])->where('coin_id', $coin_id)->update('xnb');
			$r[] = $this->db->set('num_frozen', 'num_frozen-' . $deal_num, false)->where('user_id', $d['sell_user_id'])->where('coin_id', $coin_id)->update('xnb');
			$r[] = $this->db->set('deal', 'deal+' . $deal_num, false)->where('id', $id)->update('orders');
			$r[] = $this->db->set('deal', 'deal+' . $deal_num, false)->where('id', $v['id'])->update('orders');
			$r[] = $this->db->query('update t_orders set status=1 where num<=deal and status=0');
		}

		if (correct($r)) {
			$this->db->query('commit');
		}
		else {
			$this->db->query('rollback');
		}

		$this->db->query('unlock tables');
	}

	public function cancel()
	{
		$id = intval($this->input->post('id'));

		if ($id <= 0) {
			ajax_return(1, '找不到目标');
		}

		$orders = $this->db->where('id', $id)->where('status', 0)->where('user_id', $this->user['id'])->get('orders')->row_array();

		if (!$orders) {
			ajax_return(1, '找不到目标');
		}

		$this->db->query('set autocommit=0');
		$rs = array();

		if ($orders['type'] == 0) {
			$this->db->query('lock tables t_user write,t_orders write');
			$amount = num(($orders['price'] * $orders['num']) - $orders['deal_total'], 2);
			$rs[] = $this->db->where('id', $this->user['id'])->set('cny', 'cny+' . $amount, false)->update('user');
		}
		else if ($orders['type'] == 1) {
			$this->db->query('lock tables t_xnb write,t_orders write');
			$amount = num($orders['num'] - $orders['deal'], 4);
			$rs[] = $this->db->where('user_id', $this->user['id'])->where('coin_id', $orders['coin_id'])->set('num', 'num+' . $amount, false)->update('xnb');
		}
		else {
			ajax_return(1, '找不到目标');
		}

		$rs[] = $this->db->where('id', $id)->set('status', 2)->update('orders');

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '撤销成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '撤销失败');
		}
	}
}

?>
